<?php

abstract class SysTiisController extends Controller {

    /**
     * @desc layout
     */
    public $layout;

    /**
     * @desc auth sysadmin 
     */
    public $_auth;

    /**
     * @desc menus sysadmin 
     */
    public $_menus;

    /**
     * @desc top nav sysadmin 
     */
    public $_topnav;

    public function init() {
        parent::init();
        // Load TIIS Bootstrap.
        $tiisBootstrap = Yii::createComponent(array('class' => 'application.components.TiisBootstrap'));
        Yii::app()->setComponent('bootstrap', $tiisBootstrap);
        Yii::app()->setAliases(array('bootstrap' => 'ext.bootstrap'));

        //set Theme & layout
        $this->layout = '//layouts/main';
        $theme = 'sysadmin';
        Yii::app()->setTheme(file_exists(dirname(__FILE__) . '/../../themes/' . $theme) ? $theme : 'sysadmin');
    }

    public function beforeAction($action) {
        //check authentication
        $this->_auth = Yii::app()->user->id;
        if ($action->id != 'login') {
            if (!$this->_auth)//=> yet not login
                $this->redirect(Yii::app()->baseUrl . '/sysadmin/login.html');
        }elseif ($this->_auth && $action->id == 'login') {
            $this->redirect(Yii::app()->baseUrl . '/sysadmin.html');
        }
        //set the menu
        $this->_topnav = array(
            array(
                'class' => 'bootstrap.widgets.TbMenu',
                'items' => array(
                    array('label' => 'Dashboard', 'url' => Yii::app()->baseUrl . '/sysadmin.html', 'icon' => 'icon-home icon-white', 'active' => $this->id == 'default' ? true : false),
                ),
            ),
            array(
                'class' => 'bootstrap.widgets.TbMenu',
                'htmlOptions' => array('class' => 'pull-right'),
                'items' => array(
                    array('label' => 'Hệ thống', 'url' => '#', 'icon' => 'icon-globe icon-white', 'items' => array(
                            array('label' => 'Đăng xuất', 'url' => Yii::app()->baseUrl . '/sysadmin/logout.html', 'icon' => 'icon-off', 'active' => $this->id == 'baihoc' ? true : false),
                            array('label' => 'Tin nhắn', 'url' => '#', 'icon' => 'icon-envelope'),
                            '---',
                            array('label' => 'Cấu hình', 'url' => '#', 'icon' => 'icon-wrench'),
                    )),
                ),
            ),
            array(
                'class' => 'bootstrap.widgets.TbMenu',
                'htmlOptions' => array('class' => 'pull-left'),
                'items' => array(
                    array('label' => 'Quản lý thông tin hồ sơ', 'url' => '#', 'icon' => 'icon-user icon-white', 'items' => array(
                            array('label' => 'Ngành nghề', 'url' => Yii::app()->baseUrl . '/sysadmin/nganhnghe.html', 'icon' => 'icon-minus', 'active' => $this->id == 'nganhnghe' ? true : false),
                            array('label' => 'Cấp bậc', 'url' => Yii::app()->baseUrl . '/sysadmin/capbac.html', 'icon' => 'icon-minus', 'active' => $this->id == 'capbac' ? true : false),
                            array('label' => 'Bằng cấp', 'url' => Yii::app()->baseUrl . '/sysadmin/bangcap.html', 'icon' => 'icon-minus', 'active' => $this->id == 'bangcap' ? true : false),
                            array('label' => 'Ngoại ngữ','url' =>  Yii::app()->baseUrl . '/sysadmin/ngoaingu.html', 'icon' => 'icon-minus', 'active' => $this->id == 'ngoaingu' ? true : false),
                            array('label' => 'Trình độ ngoại ngữ','url' =>  Yii::app()->baseUrl . '/sysadmin/trinhdo.html', 'icon' => 'icon-minus', 'active' => $this->id == 'trinhdo' ? true : false),
                            array('label' => 'Kinh nghiệm','url' =>  Yii::app()->baseUrl . '/sysadmin/kinhnghiem.html', 'icon' => 'icon-minus', 'active' => $this->id == 'kinhnghiem' ? true : false),
                            array('label' => 'Lương','url' =>  Yii::app()->baseUrl . '/sysadmin/luong.html', 'icon' => 'icon-minus', 'active' => $this->id == 'luong' ? true : false),
                            array('label' => 'Hình thức làm việc','url' =>  Yii::app()->baseUrl . '/sysadmin/loaihinh.html', 'icon' => 'icon-minus', 'active' => $this->id == 'hinhthuc' ? true : false),
                            array('label' => 'Loại hình làm việc','url' =>  Yii::app()->baseUrl . '/sysadmin/hinhthuc.html', 'icon' => 'icon-minus', 'active' => $this->id == 'loaihinh' ? true : false),
                            array('label' => 'Tỉnh thành','url' =>  Yii::app()->baseUrl . '/sysadmin/thanhpho.html', 'icon' => 'icon-minus', 'active' => $this->id == 'thanhpho' ? true : false),
                            array('label' => 'Quốc gia','url' =>  Yii::app()->baseUrl . '/sysadmin/quocgia.html', 'icon' => 'icon-minus', 'active' => $this->id == 'quocgia' ? true : false),
                            array('label' => 'Kỹ năng','url' =>  Yii::app()->baseUrl . '/sysadmin/kynang.html', 'icon' => 'icon-minus', 'active' => $this->id == 'kynang' ? true : false),
                    )),
                ),
            ),
        );

        //set the menu
        $this->_menus = array(
            array('label' => 'QUẢN LÝ NGƯỜI TÌM VIỆC'),
            array('label' => 'Danh sách', 'url' => '#', 'icon' => 'icon-list-alt', 'active' => $this->id == 'thamkhao' ? true : false),
            array('label' => 'Danh sách hồ sơ', 'url' => '#', 'icon' => 'icon-file', 'active' => $this->id == 'thamkhao' ? true : false),
            '---',
            array('label' => 'QUẢN LÝ NHÀ TUYỂN DỤNG'),
            array('label' => 'Danh sách', 'url' => '#', 'icon' => 'icon-list-alt', 'active' => $this->id == 'thamkhao' ? true : false),
            array('label' => 'Danh sách hồ sơ', 'url' => '#', 'icon' => 'icon-file', 'active' => $this->id == 'thamkhao' ? true : false),
            '---',
            array('label' => 'QUẢN LÝ TIN TỨC'),
            array('label' => 'Danh mục tin', 'url' => '#', 'icon' => 'icon-th-list', 'active' => $this->id == 'level' ? true : false),
            array('label' => 'Tin tức', 'url' => '#', 'icon' => 'icon-book', 'active' => $this->id == 'level' ? true : false),
        );

        return true;
    }

    /**
     * @desc login sysadmin 
     */
    public function actionLogin() {

        //set layout to login 
        $this->layout = '//layouts/login';

        $this->setPageTitle('Login Sysadmin');
        $sysLoginForm = new SysLoginForm();
        if (isset($_POST['SysLoginForm'])) {
            $sysLoginForm->attributes = $_POST['SysLoginForm'];
            if ($sysLoginForm->validate() && $sysLoginForm->login()) {
                $this->redirect(Yii::app()->baseUrl . '/sysadmin.html');
            }
        }
        $this->render('login', array('sysLoginForm' => $sysLoginForm));
    }

    /**
     * @desc logout sysadmin 
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->baseUrl . "/sysadmin.html");
    }
    
    protected function getParam($name, $default = '') {
        return Yii::app()->request->getParam($name, $default);
    }

}
